//
//  AppDelegate.swift
//  sinaWeibo
//
//  Created by liii on 16/6/13.
//  Copyright © 2016年 liii. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        SQLiteManager.sharedManager.createTable()
        
        // 注册监听者
        registerObserver()
        
        // 设置主题色
        setThemeColor()
        
        //自定义window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // ? 代表可选项, 作用是: 如果为nil, 后边的方法调用不执行
        // 如果用户未登录,进入访客视图; 如果用户已登陆, 显示欢迎界面
        window?.rootViewController = sharedUser.userLogin ? WelcomeViewController() : MainViewController()
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        return true
    }
    
    // 注册监听
    private func registerObserver() {
        /**
        在通知中心注册监听者, 监听控制器切换通知
        :param: observer   监听通知的对象
        :param: selector   监听到通知调用的方法
        :param: name       要监听的通知的名称
        :param: object     通知的发出者(为nil表示监听所有发起者, 不为nil表示只监听某一个发起者)
        */
        defaultCenter.addObserver(self, selector: "changeVc:", name: switchRootVcNotification, object: nil)
    }
    // 收到通知,切换控制器
    @objc private func changeVc(name: NSNotification) {
        
//        window?.rootViewController = PictureSelectorController()
        // 如果为nil则说明用户刚授权成功, 则进入主界面, 如果不为nil则说明用户在欢迎界面, 则进入
        window?.rootViewController = (name.object == nil) ? WelcomeViewController() : MainViewController()
    }
    // 移除监听者
    deinit {
        defaultCenter.removeObserver(self)
    }
    
    // 设置主题颜色
    private func setThemeColor() {
        // 导航栏主题颜色
        UINavigationBar.appearance().tintColor = themeColor
        // 工具栏主题
        UITabBar.appearance().tintColor = themeColor
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

